#pragma once
#ifndef xDevice_h__
#define xDevice_h__

#include "./xRes.h"
namespace Sky
{
	class xDxApp;
	class xDevice : public xAppRes<LPDIRECT3DDEVICE9>
	{
	public:
		xDevice();
		~xDevice();
	public:
		//xRes
		virtual TResult onCreate(xDxApp* app);
		virtual TResult onDelete(xDxApp* app);
	
		xDevice& setAdapterOrdinal(TUint Ordinal = 0){m_AdapterOrdinal = Ordinal;return *this;}
		TUint	 getAdapterOrdinal(){return m_AdapterOrdinal;}

		xDevice& setDevType(D3DDEVTYPE D3DDEVTYPE_ = D3DDEVTYPE_HAL){m_DeviceType = D3DDEVTYPE_;return *this;}
		D3DDEVTYPE getDevType(){return m_DeviceType;}

		xDevice& setBehaviorFlags(TUint D3DCREATE_ = D3DCREATE_PUREDEVICE){m_BehaviorFlags = D3DCREATE_;return *this;}
		TUint	 getBehaviorFlags(){return m_BehaviorFlags;}

		xDevice& setAdapterFormat(D3DFORMAT D3DFMT_ = D3DFMT_A8R8G8B8){m_AdapterFormat = D3DFMT_;return *this;}
		D3DFORMAT getAdapterFormat(){return m_AdapterFormat;}

		xDevice& setBackBufferWidth(TUint width = 1024){m_PresentParams.BackBufferWidth = width;return *this;}
		TUint getBackBufferWidth(){return m_PresentParams.BackBufferWidth;}

		xDevice& setBackBufferHeight(TUint height = 768){m_PresentParams.BackBufferHeight = height;return *this;}
		TUint    getBackBufferHeight(){return m_PresentParams.BackBufferHeight;}

		xDevice& setBackBufferCount(TInt cnt = 1){m_PresentParams.BackBufferCount = cnt;return *this;}
		TInt	 getBackBufferCount(){return m_PresentParams.BackBufferCount;}

		xDevice& setBackBufferFormat(D3DFORMAT D3DFMT_ = D3DFMT_UNKNOWN){m_PresentParams.BackBufferFormat =  D3DFMT_;return *this;}
		D3DFORMAT getBackBufferFormat(){return m_PresentParams.BackBufferFormat;}

		xDevice& setAutoDepthStencilFormat(D3DFORMAT D3DFMT_ = D3DFMT_D24S8){m_PresentParams.AutoDepthStencilFormat = D3DFMT_;return *this;}
		D3DFORMAT getAutoDepthStencilFormat(){return m_PresentParams.AutoDepthStencilFormat;}

		xDevice& setEnableAutoDepthStencil(TBool enable=vTrue){m_PresentParams.EnableAutoDepthStencil = enable;return *this;}
		TBool getIsEnableAutoDepthStencil(){return m_PresentParams.EnableAutoDepthStencil;}

		xDevice& setDeviceWindow(HWND window){m_PresentParams.hDeviceWindow = window;return *this;}
		HWND	getDeviceWindow(){return m_PresentParams.hDeviceWindow;}

		xDevice& setMultiSampleQuality(TInt quality = 1){m_PresentParams.MultiSampleQuality = quality;return *this;}
		TInt getMultiSampleQuality(){return m_PresentParams.MultiSampleQuality;}

		xDevice& setMultiSampleType(D3DMULTISAMPLE_TYPE D3DMULTISAMPLE_ = D3DMULTISAMPLE_7_SAMPLES){m_PresentParams.MultiSampleType = D3DMULTISAMPLE_;return *this;}
		D3DMULTISAMPLE_TYPE getMultiSampleType(){return m_PresentParams.MultiSampleType;}

		xDevice& setPresentationInterval(TUint D3DPRESENT_INTERVAL_ = D3DPRESENT_INTERVAL_DEFAULT){m_PresentParams.PresentationInterval = D3DPRESENT_INTERVAL_;return *this;}
		TUint getPresentationInterval(){return m_PresentParams.PresentationInterval ;}

		xDevice& setSwapEffect(D3DSWAPEFFECT D3DSWAPEFFECT_ = D3DSWAPEFFECT_DISCARD){m_PresentParams.SwapEffect = D3DSWAPEFFECT_;return *this;}
		D3DSWAPEFFECT getSwapEffect(){return m_PresentParams.SwapEffect;}

		xDevice& setWindowed(TBool bWindowed = vTrue){m_PresentParams.Windowed = bWindowed;return *this;}
		TBool getIsWindowed(){return m_PresentParams.Windowed;}

		xDevice& setFlag(TUint flag = 0){m_PresentParams.Flags = flag;return *this;}
		TUint getFlag(){return m_PresentParams.Flags;}

		xDevice& setFullScreen_RefreshRateInHz(TUint D3DPRESENT_RATE_ = D3DPRESENT_RATE_DEFAULT){m_PresentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_;return *this;}
		TUint getFullScreen_RefreshRateInHz(){return m_PresentParams.FullScreen_RefreshRateInHz;}
	public:
		LPDIRECT3DDEVICE9 getDevice9();
		TInt	Release();
		TResult Reset();
		TVoid	Initialise();
		TVoid	Finalise();
	private:
		friend class xApp;

		//////////////////////////////////////////////////////////////////////////
		D3DCAPS9				m_D3DCaps;
		D3DSURFACE_DESC			m_BackBufferSurfaceDesc;   // back buffer surface description
		UINT					m_AdapterOrdinal;
		D3DDEVTYPE				m_DeviceType;
		D3DFORMAT				m_AdapterFormat;
		TUint					m_BehaviorFlags;
		D3DPRESENT_PARAMETERS	m_PresentParams;
	};
}
#endif // xDevice_h__